<?php 
namespace Home\Controller;
use Hdphp\Library\Controller;

/**
*  搜索
*/
class SearchController extends CommonController
{
	

	public function index(){

		if(!empty($_POST['searcon'])){

			$content=htmlspecialchars($_POST['searcon']);

			//关联搜索 ask answer user category表 
			// 通过answer的asid 关联ask 在通过ask的uid，cid关联 user和category
			
			$sql="SELECT an.content as ancontent,a.content as acontent,an.time as antime,a.time as atime,an.*,a.*,u.*,an.uid as anuid,a.uid as auid,u.uid as uuid,c.*,a.answer as aanswer,u.answer as uanswer FROM hd_answer as an JOIN hd_ask as a JOIN hd_user as u ON an.asid=a.asid AND an.uid=u.uid JOIN hd_category as c ON a.cid=c.cid WHERE an.content like '%{$content}%'";
			$searchArr=M()->query($sql);

			//把搜索的颜色变红
			foreach ($searchArr as $key => $value) {
				$newstr="<b style='color:red;'>".$content."</b>";
				$searchArr[$key]=str_replace($content, $newstr, $value);
			}
			// p($searchArr);die;
			$this->assign('searchArr',$searchArr);
			
		}

		$this->display('search.html');
	}
}

 ?>